﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Slja.Lib;

namespace Slja.WEB
{
    public partial class ChangePassword : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            btn_submit.Attributes.Add("onclick", "return CheckPassword()");            
            if (!IsPostBack)
            {
                if (!SessionBox.CheckUserSession())
                {
                    Response.Redirect("~/login.aspx");
                }
            }
        }

        protected void btn_submit_Click(object sender, EventArgs e)
        {

            if (Request.Form["txt_password"] != "" && Request.Form["txt_newpassword"] != "" && Request.Form["txt_renewpassword"] != "")
            {
                Slja.BLL.Users bll = new Slja.BLL.Users();
                int id = SessionBox.GetUserSession().LoginId;
                if (bll.VerifyPassword(id, SecurityEncryption.MD5(Request.Form["txt_password"].Trim().ToLower(), 32)))
                {
                    //两次密码必须输入必须相同
                    if (String.Compare(Request.Form["txt_newpassword"].Trim(), Request.Form["txt_renewpassword"].Trim(), true) == 0)
                    {
                        if (bll.ChangePassword(id, SecurityEncryption.MD5(Request.Form["txt_renewpassword"].Trim().ToLower(), 32)))
                        {
                           JScript.Alert("密码修改成功!");                            
                        }
                        else
                        {
                            JScript.Alert("密码修改失败!");
                        }
                    }
                }
                else
                {
                    JScript.Alert("原密码错误!");
                }
            }
        }
    }
}
